package demo.practice.competition;

import java.util.Arrays;

public class P5641_2 {
    public static void main(String[] args) {
        P5641_2 p5641_2= new P5641_2();
        p5641_2.maximumUnits(new int[][]{{1,3},{2,2},{3,1}},  4);
    }
    public int maximumUnits(int[][] boxTypes, int truckSize) {

        Arrays.sort(boxTypes,(o1, o2) ->{
            return o2[1]-o1[1];
        });

        int sum=0;

        for(int i=0;i<boxTypes.length&&truckSize>0;i++){
                if(boxTypes[i][0]<=truckSize){
                    //箱子数还不够
                    sum+=boxTypes[i][0]*boxTypes[i][1];
                    truckSize-=boxTypes[i][0];
                }else{
                    //箱子数足够了
                    sum+=truckSize*boxTypes[i][1];
                    break;
                }
        }
        return sum;

       /* while (truckSize>0&&priorityQueue.size()>0){
            Kv poll = priorityQueue.poll();
            if(poll.getK()<=truckSize){
                //
                sum+=poll.getK()*poll.getV();
                truckSize=truckSize-poll.getK();
            }else{
                sum+=truckSize*poll.getV();
                break;
            }
        }
        return sum;*/
    }

}
